"use strict";

/*
 * MikoPBX - free phone system for small business
 * Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program.
 * If not, see <https://www.gnu.org/licenses/>.
 */

/* global globalRootUrl, globalWebAdminLanguage, sessionStorage, $, globalTranslate */

/**
 * Advice Worker module.
 * @module adviceWorker
 */
var adviceWorker = {
  /**
   * Time in milliseconds before fetching new advice.
   * @type {number}
   */
  timeOut: 10000,

  /**
   * The id of the timer function for advice worker.
   * @type {number}
   */
  timeOutHandle: 0,

  /**
   * jQuery element for advice container.
   * @type {jQuery}
   */
  $advice: $('#advice'),

  /**
   * jQuery element for advice bell button.
   * @type {jQuery}
   */
  $adviceBellButton: $('#show-advice-button'),

  /**
   * Initializes the advice worker.
   */
  initialize: function initialize() {
    adviceWorker.showPreviousAdvice(); // Let's initiate the retrieval of new advice.

    adviceWorker.restartWorker();
    window.addEventListener('ConfigDataChanged', adviceWorker.cbOnDataChanged);
  },

  /**
   * Restarts the advice worker.
   */
  restartWorker: function restartWorker() {
    window.clearTimeout(adviceWorker.timeoutHandle);
    adviceWorker.worker();
  },

  /**
   * Event handler for language or data change.
   */
  cbOnDataChanged: function cbOnDataChanged() {
    sessionStorage.removeItem("previousAdvice".concat(globalWebAdminLanguage));
    sessionStorage.removeItem("previousAdviceBell".concat(globalWebAdminLanguage));
    setTimeout(adviceWorker.restartWorker, 3000);
  },

  /**
   * Shows old advice until receiving an update from the station.
   */
  showPreviousAdvice: function showPreviousAdvice() {
    var previousAdviceBell = sessionStorage.getItem("previousAdviceBell".concat(globalWebAdminLanguage));

    if (previousAdviceBell) {
      adviceWorker.$adviceBellButton.html(previousAdviceBell);
    }

    var previousAdvice = sessionStorage.getItem("previousAdvice".concat(globalWebAdminLanguage));

    if (previousAdvice) {
      adviceWorker.$advice.html(previousAdvice);
      adviceWorker.$adviceBellButton.popup({
        position: 'bottom left',
        popup: adviceWorker.$advice,
        delay: {
          show: 300,
          hide: 10000
        },
        on: 'click',
        movePopup: false
      });
    }
  },

  /**
   * Worker function for fetching advice.
   */
  worker: function worker() {
    PbxApi.AdviceGetList(adviceWorker.cbAfterResponse);
  },

  /**
   * Callback function after receiving the response.
   * @param {object} response - Response object from the API.
   */
  cbAfterResponse: function cbAfterResponse(response) {
    if (response === false) {
      return;
    }

    adviceWorker.$advice.html('');

    if (response.advice !== undefined) {
      var htmlMessages = '';
      var countMessages = 0;
      var iconBellClass = '';
      htmlMessages += "<div class=\"ui header\">".concat(globalTranslate.adv_PopupHeader, "</div>");
      htmlMessages += '<div class="ui relaxed divided list">';

      if (response.advice.needUpdate !== undefined && response.advice.needUpdate.length > 0) {
        $(window).trigger('SecurityWarning', [response.advice]);
      }

      if (response.advice.error !== undefined && response.advice.error.length > 0) {
        $.each(response.advice.error, function (key, value) {
          htmlMessages += '<div class="item">';
          htmlMessages += '<i class="frown outline red icon"></i>';
          htmlMessages += "".concat(value);
          htmlMessages += '</div>';
          countMessages += 1;
        });
      }

      if (response.advice.warning !== undefined && response.advice.warning.length > 0) {
        $.each(response.advice.warning, function (key, value) {
          htmlMessages += '<div class="item yellow">';
          htmlMessages += '<i class="meh outline yellow icon"></i>';
          htmlMessages += "".concat(value);
          htmlMessages += '</div>';
          countMessages += 1;
        });
      }

      if (response.advice.info !== undefined && response.advice.info.length > 0) {
        $.each(response.advice.info, function (key, value) {
          htmlMessages += '<div class="item">';
          htmlMessages += '<i class="smile outline blue icon"></i>';
          htmlMessages += "".concat(value);
          htmlMessages += '</div>';
          countMessages += 1;
        });
      }

      if (response.advice.error !== undefined && response.advice.error.length > 0) {
        iconBellClass = 'red icon bell';
      } else if (response.advice.warning !== undefined && response.advice.warning.length > 0) {
        iconBellClass = 'yellow icon bell';
      } else if (response.advice.info !== undefined && response.advice.info.length > 0) {
        iconBellClass = 'blue icon bell';
      }

      htmlMessages += '</div>';
      adviceWorker.$advice.html(htmlMessages);
      sessionStorage.setItem("previousAdvice".concat(globalWebAdminLanguage), htmlMessages);

      if (countMessages > 0) {
        adviceWorker.$adviceBellButton.html("<i class=\"".concat(iconBellClass, "\"></i>").concat(countMessages)).popup({
          position: 'bottom left',
          popup: adviceWorker.$advice,
          delay: {
            show: 300,
            hide: 10000
          },
          on: 'click',
          movePopup: false
        });
        adviceWorker.$adviceBellButton.find('i').transition('set looping').transition('pulse', '1000ms');
      } else {
        adviceWorker.$adviceBellButton.html("<i class=\"grey icon bell\"></i>");
      }

      sessionStorage.setItem("previousAdviceBell".concat(globalWebAdminLanguage), adviceWorker.$adviceBellButton.html());
      adviceWorker.timeoutHandle = window.setTimeout(adviceWorker.worker, adviceWorker.timeOut);
    } else if (response.success === true && response.advice !== undefined && response.advice.length === 0) {
      sessionStorage.removeItem("previousAdvice".concat(globalWebAdminLanguage));
      sessionStorage.removeItem("previousAdviceBell".concat(globalWebAdminLanguage));
      adviceWorker.$adviceBellButton.html('<i class="grey icon bell outline"></i>');
    }
  }
};
/**
 *  Initialize advice worker on document ready
 */

$(document).ready(function () {
  adviceWorker.initialize();
});
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9BZHZpY2UvYWR2aWNlLXdvcmtlci5qcyJdLCJuYW1lcyI6WyJhZHZpY2VXb3JrZXIiLCJ0aW1lT3V0IiwidGltZU91dEhhbmRsZSIsIiRhZHZpY2UiLCIkIiwiJGFkdmljZUJlbGxCdXR0b24iLCJpbml0aWFsaXplIiwic2hvd1ByZXZpb3VzQWR2aWNlIiwicmVzdGFydFdvcmtlciIsIndpbmRvdyIsImFkZEV2ZW50TGlzdGVuZXIiLCJjY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widWkgcmVsYXhlZCBkaXZpZGVkIGxpc3RcIj4nO1xuXG4gICAgICAgICAgICBpZiAocmVzcG9uc2UuYWR2aWNlLm5lZWRVcGRhdGUgIT09IHVuZGVmaW5lZFxuICAgICAgICAgICAgICAgICYmIHJlc3BvbnNlLmFkdmljZS5uZWVkVXBkYXRlLmxlbmd0aCA+IDApIHtcbiAgICAgICAgICAgICAgICAkKHdpbmRvdykudHJpZ2dlcignU2VjdXJpdHlXYXJuaW5nJywgW3Jlc3BvbnNlLmFkdmljZV0pO1xuICAgICAgICAgICAgfVxuXG4gICAgICAgICAgICBpZiAocmVzcG9uc2UuYWR2aWNlLmVycm9yICE9PSB1bmRlZmluZWRcbiAgICAgICAgICAgICAgICAmJiByZXNwb25zZS5hZHZpY2UuZXJyb3IubGVuZ3RoID4gMCkge1xuICAgICAgICAgICAgICAgICQuZWFjaChyZXNwb25zZS5hZHZpY2UuZXJyb3IsIChrZXksIHZhbHVlKSA9PiB7XG4gICAgICAgICAgICAgICAgICAgIGh0bWxNZXNzYWdlcyArPSAnPGRpdiBjbGFzcz1cIml0ZW1cIj4nO1xuICAgICAgICAgICAgICAgICAgICBodG1sTWVzc2FnZXMgKz0gJzxpIGNsYXNzPVwiZnJvd24gb3V0bGluZSByZWQgaWNvblwiPjwvaT4nO1xuICAgICAgICAgICAgICAgICAgICBodG1sTWVzc2FnZXMgKz0gYCR7dmFsdWV9YDtcbiAgICAgICAgICAgICAgICAgICAgaHRtbE1lc3NhZ2VzICs9ICc8L2Rpdj4nO1xuICAgICAgICAgICAgICAgICAgICBjb3VudE1lc3NhZ2VzICs9IDE7XG4gICAgICAgICAgICAgICAgfSk7XG4gICAgICAgICAgICB9XG4gICAgICAgICAgICBpZiAocmVzcG9uc2UuYWR2aWNlLndhcm5pbmcgIT09IHVuZGVmaW5lZFxuICAgICAgICAgICAgICAgICYmIHJlc3BvbnNlLmFkdmljZS53YXJuaW5nLmxlbmd0aCA+IDApIHtcbiAgICAgICAgICAgICAgICAkLmVhY2gocmVzcG9uc2UuYWR2aWNlLndhcm5pbmcsIChrZXksIHZhbHVlKSA9PiB7XG4gICAgICAgICAgICAgICAgICAgIGh0bWxNZXNzYWdlcyArPSAnPGRpdiBjbGFzcz1cIml0ZW0geWVsbG93XCI+JztcbiAgICAgICAgICAgICAgICAgICAgaHRtbE1lc3NhZ2VzICs9ICc8aSBjbGFzcz1cIm1laCBvdXRsaW5lIHllbGxvdyBpY29uXCI+PC9pPic7XG4gICAgICAgICAgICAgICAgICAgIGh0bWxNZXNzYWdlcyArPSBgJHt2YWx1ZX1gO1xuICAgICAgICAgICAgICAgICAgICBodG1sTWVzc2FnZXMgKz0gJzwvZGl2Pic7XG4gICAgICAgICAgICAgICAgICAgIGNvdW50TWVzc2FnZXMgKz0gMTtcbiAgICAgICAgICAgICAgICB9KTtcbiAgICAgICAgICAgIH1cbiAgICAgICAgICAgIGlmIChyZXNwb25zZS5hZHZpY2UuaW5mbyAhPT0gdW5kZWZpbmVkXG4gICAgICAgICAgICAgICAgJiYgcmVzcG9uc2UuYWR2aWNlLmluZm8ubGVuZ3RoID4gMCkge1xuICAgICAgICAgICAgICAgICQuZWFjaChyZXNwb25zZS5hZHZpY2UuaW5mbywgKGtleSwgdmFsdWUpID0+IHtcbiAgICAgICAgICAgICAgICAgICAgaHRtbE1lc3NhZ2VzICs9ICc8ZGl2IGNsYXNzPVwiaXRlbVwiPic7XG4gICAgICAgICAgICAgICAgICAgIGh0bWxNZXNzYWdlcyArPSAnPGkgY2xhc3M9XCJzbWlsZSBvdXRsaW5lIGJsdWUgaWNvblwiPjwvaT4nO1xuICAgICAgICAgICAgICAgICAgICBodG1sTWVzc2FnZXMgKz0gYCR7dmFsdWV9YDtcbiAgICAgICAgICAgICAgICAgICAgaHRtbE1lc3NhZ2VzICs9ICc8L2Rpdj4nO1xuICAgICAgICAgICAgICAgICAgICBjb3VudE1lc3NhZ2VzICs9IDE7XG4gICAgICAgICAgICAgICAgfSk7XG4gICAgICAgICAgICB9XG5cbiAgICAgICAgICAgIGlmIChyZXNwb25zZS5hZHZpY2UuZXJyb3IgIT09IHVuZGVmaW5lZFxuICAgICAgICAgICAgICAgICYmIHJlc3BvbnNlLmFkdmljZS5lcnJvci5sZW5ndGggPiAwKSB7XG4gICAgICAgICAgICAgICAgaWNvbkJlbGxDbGFzcyA9ICdyZWQgaWNvbiBiZWxsJztcbiAgICAgICAgICAgIH0gZWxzZSBpZiAocmVzcG9uc2UuYWR2aWNlLndhcm5pbmcgIT09IHVuZGVmaW5lZFxuICAgICAgICAgICAgICAgICYmIHJlc3BvbnNlLmFkdmljZS53YXJuaW5nLmxlbmd0aCA+IDApIHtcbiAgICAgICAgICAgICAgICBpY29uQmVsbENsYXNzID0gJ3llbGxvdyBpY29uIGJlbGwnO1xuXG4gICAgICAgICAgICB9IGVsc2UgaWYgKHJlc3BvbnNlLmFkdmljZS5pbmZvICE9PSB1bmRlZmluZWRcbiAgICAgICAgICAgICAgICAmJiByZXNwb25zZS5hZHZpY2UuaW5mby5sZW5ndGggPiAwKSB7XG4gICAgICAgICAgICAgICAgaWNvbkJlbGxDbGFzcyA9ICdibHVlIGljb24gYmVsbCc7XG4gICAgICAgICAgICB9XG4gICAgICAgICAgICBodG1sTWVzc2FnZXMgKz0gJzwvZGl2Pic7XG4gICAgICAgICAgICBhZHZpY2VXb3JrZXIuJGFkdmljZS5odG1sKGh0bWxNZXNzYWdlcyk7XG4gICAgICAgICAgICBzZXNzaW9uU3RvcmFnZS5zZXRJdGVtKGBwcmV2aW91c0FkdmljZSR7Z2xvYmFsV2ViQWRtaW5MYW5ndWFnZX1gLCBodG1sTWVzc2FnZXMpO1xuXG4gICAgICAgICAgICBpZiAoY291bnRNZXNzYWdlcyA+IDApIHtcbiAgICAgICAgICAgICAgICBhZHZpY2VXb3JrZXIuJGFkdmljZUJlbGxCdXR0b25cbiAgICAgICAgICAgICAgICAgICAgLmh0bWwoYDxpIGNsYXNzPVwiJHtpY29uQmVsbENsYXNzfVwiPjwvaT4ke2NvdW50TWVzc2FnZXN9YClcbiAgICAgICAgICAgICAgICAgICAgLnBvcHVwKHtcbiAgICAgICAgICAgICAgICAgICAgICAgIHBvc2l0aW9uOiAnYm90dG9tIGxlZnQnLFxuICAgICAgICAgICAgICAgICAgICAgICAgcG9wdXA6IGFkdmljZVdvcmtlci4kYWR2aWNlLFxuICAgICAgICAgICAgICAgICAgICAgICAgZGVsYXk6IHtcbiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzaG93OiAzMDAsXG4gICAgICAgICAgICAgICAgICAgICAgICAgICAgaGlkZTogMTAwMDAsXG4gICAgICAgICAgICAgICAgICAgICAgICB9LFxuICAgICAgICAgICAgICAgICAgICAgICAgb246ICdjbGljaycsXG4gICAgICAgICAgICAgICAgICAgICAgICBtb3ZlUG9wdXA6IGZhbHNlLFxuICAgICAgICAgICAgICAgICAgICB9KTtcbiAgICAgICAgICAgICAgICBhZHZpY2VXb3JrZXIuJGFkdmljZUJlbGxCdXR0b24uZmluZCgnaScpXG4gICAgICAgICAgICAgICAgICAgIC50cmFuc2l0aW9uKCdzZXQgbG9vcGluZycpXG4gICAgICAgICAgICAgICAgICAgIC50cmFuc2l0aW9uKCdwdWxzZScsICcxMDAwbXMnKTtcbiAgICAgICAgICAgIH0gZWxzZSB7XG4gICAgICAgICAgICAgICAgYWR2aWNlV29ya2VyLiRhZHZpY2VCZWxsQnV0dG9uXG4gICAgICAgICAgICAgICAgICAgIC5odG1sKGA8aSBjbGFzcz1cImdyZXkgaWNvbiBiZWxsXCI+PC9pPmApXG4gICAgICAgICAgICB9XG4gICAgICAgICAgICBzZXNzaW9uU3RvcmFnZS5zZXRJdGVtKGBwcmV2aW91c0FkdmljZUJlbGwke2dsb2JhbFdlYkFkbWluTGFuZ3VhZ2V9YCwgYWR2aWNlV29ya2VyLiRhZHZpY2VCZWxsQnV0dG9uLmh0bWwoKSk7XG4gICAgICAgICAgICBhZHZpY2VXb3JrZXIudGltZW91dEhhbmRsZSA9IHdpbm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